package io.renren.service.impl;

import io.renren.dao.SysAreaDao;
import io.renren.entity.SysAreaEntity;
import io.renren.entity.VO.SysAreaEntityVO;
import io.renren.entity.VO.SysOfficeEntityVO;
import io.renren.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;



@Service("sysAreaService")
public class SysAreaServiceImpl implements SysAreaService {
	@Autowired
	private SysAreaDao sysAreaDao;
	
	@Override
	public SysAreaEntity queryObject(Long areaId){
		return sysAreaDao.queryObject(areaId);
	}
	
	@Override
	public List<SysAreaEntity> queryList(Map<String, Object> map){
		return sysAreaDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return sysAreaDao.queryTotal(map);
	}
	
	@Override
	public void save(SysAreaEntity sysArea){
		sysAreaDao.save(sysArea);
	}
	
	@Override
	public void update(SysAreaEntity sysArea){
		sysAreaDao.update(sysArea);
	}
	
	@Override
	public void delete(Long areaId){
		sysAreaDao.delete(areaId);
	}
	
	@Override
	public void deleteBatch(Long[] areaIds){
		sysAreaDao.deleteBatch(areaIds);
	}

    @Override
    public int findSubCount(Long areaId) {
        return sysAreaDao.findSubCount(areaId);
    }

	@Override
	public SysAreaEntityVO getTree() {
		SysAreaEntityVO vo = getSingleTree("1");
		return vo;
	}

	@Override
	public void updateArea(String areaId, String parentareaId) {
		sysAreaDao.updateArea(areaId,parentareaId);
	}

	public SysAreaEntityVO getSingleTree(String id){
		SysAreaEntityVO vo = sysAreaDao.getTreeById(id);
		List<SysAreaEntityVO> childTreeNodes  = sysAreaDao.getTreeByParentid(vo.getAreaId()+"");
		for (SysAreaEntityVO inner : childTreeNodes ){
			SysAreaEntityVO n = getSingleTree(inner.getAreaId()+"");
			vo.getChildren().add(n);
			//vo.getList().add(n);
		}
		return vo;
	}

}
